 package handling.channel.handler;

 import client.MapleBuffStat;
 import client.MapleCharacter;
 import client.MapleClient;
 import client.PlayerStats;
 import client.inventory.Item;
 import client.inventory.MapleInventoryType;
 import client.skills.Skill;
 import client.skills.SkillFactory;
 import client.status.MonsterStatus;
 import client.status.MonsterStatusEffect;
 import configs.ServerConfig;
 import constants.ItemConstants;
 import constants.JobConstants;
 import java.awt.Point;
 import java.util.EnumMap;
 import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.MapleInventoryManipulator;
 import server.MapleStatEffect;
 import server.life.MapleLifeFactory;
 import server.life.MapleMonster;
 import server.life.MobAttackInfo;
 import server.life.MobSkill;
 import server.life.MobSkillFactory;
 import server.maps.MapleMapObject;
 import tools.MaplePacketCreator;
 import tools.Pair;
 import tools.Randomizer;
 import tools.data.input.LittleEndianAccessor;
 import tools.packet.BuffPacket;
 import tools.packet.EffectPacket;









 public class TakeDamageHandler
 {
/*  47 */   private static final Logger log = LogManager.getLogger(TakeDamageHandler.class.getName());




   public static void TakeDamage(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/*  53 */     if (chr == null || chr.getMap() == null || chr.isHidden() || (chr.isGM() && chr.isInvincible())) {
/*  54 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  57 */     slea.readInt();
/*  58 */     slea.readInt();
/*  59 */     chr.updateTick(slea.readInt());
/*  60 */     byte type = slea.readByte();
/*  61 */     slea.skip(1);
/*  62 */     int damage = slea.readInt();
/*  63 */     slea.skip(6);
/*  64 */     boolean isDeadlyAttack = false;
/*  65 */     boolean pPhysical = false;
/*  66 */     int oid = 0;
/*  67 */     int monsteridfrom = 0;
/*  68 */     int fake = 0;
/*  69 */     int mpattack = 0;
/*  70 */     byte direction = 0;
/*  71 */     int skillId = 0;
/*  72 */     int pOid = 0;
/*  73 */     int pDamage = 0;
/*  74 */     byte pType = 0;
/*  75 */     byte defType = 0;
/*  76 */     Point pPos = new Point(0, 0);
/*  77 */     MapleMonster attacker = null;
/*  78 */     PlayerStats stats = chr.getStat();
/*  79 */     if (chr.isShowPacket()) {
/*  80 */       chr.dropDebugMessage(1, "[玩家受伤] 受伤类型: " + type + " 受伤数值: " + damage);
     }
/*  82 */     switch (type) {
       case -8:
       case -5:
       case -4:
       case -3:
       case -2:
         break;

       default:
/*  91 */         monsteridfrom = slea.readInt();
/*  92 */         oid = slea.readInt();
/*  93 */         attacker = chr.getMap().getMonsterByOid(oid);
/*  94 */         direction = slea.readByte();


/*  97 */         if (attacker == null || attacker.getId() != monsteridfrom || attacker.getLinkCID() > 0 || attacker.isFake() || attacker.getStats().isFriendly()) {
           return;
         }
/* 100 */         if (type != -1 && damage > 0) {
/* 101 */           List<MapleLifeFactory.loseItem> loseItems = chr.getMap().getMonsterById(monsteridfrom).getStats().loseItem();
/* 102 */           if (loseItems != null) {

/* 104 */             int playerpos = (chr.getPosition()).x;
/* 105 */             byte d = 1;
/* 106 */             Point pos = new Point(0, (chr.getPosition()).y);
/* 107 */             for (MapleLifeFactory.loseItem loseItem : loseItems) {
/* 108 */               MapleInventoryType InvType = ItemConstants.getInventoryType(loseItem.getId()); byte b;
/* 109 */               for (b = 0; b < loseItem.getX(); b = (byte)(b + 1)) {
/* 110 */                 if (Randomizer.nextInt(101) >= loseItem.getChance()) {
/* 111 */                   if (chr.haveItem(loseItem.getId())) {
/* 112 */                     pos.x = playerpos + ((d % 2 == 0) ? (25 * (d + 1) / 2) : -(25 * d / 2));
/* 113 */                     MapleInventoryManipulator.removeById(c, InvType, loseItem.getId(), 1, false, false);
/* 114 */                     chr.getMap().spawnItemDrop((MapleMapObject)c.getPlayer(), c.getPlayer(), new Item(loseItem.getId(), (short)0, (short)1), chr.getMap().calcDropPos(pos, chr.getPosition()), true, true);
/* 115 */                     d = (byte)(d + 1);
                   } else {
                     break;
                   }
                 }
               }
             }
/* 122 */             chr.getMap().removeMapObject((MapleMapObject)attacker);
           }
/* 124 */           MobAttackInfo attackInfo = attacker.getStats().getMobAttack(type);
/* 125 */           if (attackInfo != null) {
/* 126 */             if (attackInfo.isElement && stats.TER > 0 && Randomizer.nextInt(100) < stats.TER) {
               return;
             }

/* 130 */             if (attackInfo.isDeadlyAttack()) {
/* 131 */               isDeadlyAttack = true;
/* 132 */               mpattack = stats.getMp() - 1;
             } else {
/* 134 */               mpattack += attackInfo.getMpBurn();
             }
/* 136 */             MobSkill skill = MobSkillFactory.getMobSkill(attackInfo.getDiseaseSkill(), attackInfo.getDiseaseLevel());
/* 137 */             if (skill != null && (damage == -1 || damage > 0) && skill.getDuration() != 0L) {
/* 138 */               skill.applyEffect(chr, attacker, 0, false);
             }
/* 140 */             attacker.setMp(attacker.getMp() - attackInfo.getMpCon());
           }
         }


/* 145 */         skillId = slea.readInt();
/* 146 */         pDamage = slea.readInt();
/* 147 */         defType = slea.readByte();
/* 148 */         slea.skip(1);
/* 149 */         if (chr.isShowPacket()) {
/* 150 */           chr.dropDebugMessage(1, "[玩家受伤] 受到伤害: " + damage + " 技能ID: " + skillId + " 反射伤害: " + pDamage + " defType: " + defType);
         }
/* 152 */         if (skillId != 0 && pDamage > 0) {
/* 153 */           pPhysical = (slea.readByte() > 0);
/* 154 */           pOid = slea.readInt();
/* 155 */           pType = slea.readByte();
/* 156 */           slea.skip(4);
/* 157 */           pPos = slea.readPos();
         }
         break;
     }
/* 161 */     if (defType == 1) {
/* 162 */       Skill bx = SkillFactory.getSkill(31110008);
/* 163 */       int bof = chr.getTotalSkillLevel(bx);
/* 164 */       if (bof > 0) {
/* 165 */         MapleStatEffect eff = bx.getEffect(bof);
/* 166 */         chr.addDemonMp(eff.getZ());
/* 167 */         chr.addHP(chr.getStat().getCurrentMaxHp() / 100 * eff.getY());
       }
     }
/* 170 */     if (damage == -1) {
/* 171 */       fake = 4020002 + (chr.getJob() / 10 - 40) * 100000;
/* 172 */       if (fake != 4120002 && fake != 4220002) {
/* 173 */         fake = 4120002;
       }
/* 175 */       if (type == -1) {
/* 176 */         if (chr.getJob() == 122 && chr.getInventory(MapleInventoryType.EQUIPPED).getItem((short)-10) != null) {
/* 177 */           if (chr.getTotalSkillLevel(1221016) > 0) {
/* 178 */             MapleStatEffect eff = SkillFactory.getSkill(1221016).getEffect(chr.getTotalSkillLevel(1221016));
/* 179 */             attacker.applyStatus(chr, new MonsterStatusEffect(MonsterStatus.MOB_STAT_Stun, Integer.valueOf(1), 1221016, null, false, 0), false, eff.getDuration(), true, eff);
/* 180 */             fake = 1221016;
           }
/* 182 */         } else if (chr.getJob() == 3611 || chr.getJob() == 3612) {
/* 183 */           MapleStatEffect effect = chr.getStatForBuff(MapleBuffStat.黑暗高潮);
/* 184 */           if (effect != null && effect.getSourceid() == 36111003) {
/* 185 */             int prop = chr.getBuffedValue(MapleBuffStat.黑暗高潮).intValue();
/* 186 */             if (prop > 0) {
/* 187 */               prop -= effect.getY();
             }
/* 189 */             int z = chr.getBuffedValue(MapleBuffStat.伤害吸收).intValue();
/* 190 */             if (z > 0) {
/* 191 */               z += effect.getZ();
             }
/* 193 */             int x = effect.getX();
/* 194 */             x -= (effect.getProp() - prop) / effect.getY();
/* 195 */             if (x > 0 && prop > 0) {
/* 196 */               Map<MapleBuffStat, Integer> buffstats = new EnumMap<>(MapleBuffStat.class);
/* 197 */               buffstats.put(MapleBuffStat.黑暗高潮, Integer.valueOf(prop));
/* 198 */               buffstats.put(MapleBuffStat.伤害吸收, Integer.valueOf(z));
/* 199 */               chr.setBuffedValue(MapleBuffStat.黑暗高潮, prop);
/* 200 */               chr.setBuffedValue(MapleBuffStat.伤害吸收, z);
/* 201 */               int duration = effect.getDuration();
/* 202 */               chr.send(BuffPacket.giveBuff(effect.getSourceid(), duration, buffstats, effect, chr));
             } else {
/* 204 */               chr.dispelSkill(36111003);
             }
/* 206 */             fake = 36111003;
           }
/* 208 */         } else if (JobConstants.is剑豪(chr.getJob())) {
/* 209 */           int skilllevel = chr.getSkillLevel(41110006);

/* 211 */           if (skilllevel > 0) {
/* 212 */             MapleStatEffect eff = SkillFactory.getSkill(41110006).getEffect(skilllevel);
/* 213 */             int prop = eff.getProp();
/* 214 */             if (Randomizer.nextInt(100) < prop) {
/* 215 */               eff.applyTo(chr);
             }
           }
/* 218 */           skilllevel = chr.getSkillLevel(41120006);
/* 219 */           if (skilllevel > 0) {
/* 220 */             MapleStatEffect eff = SkillFactory.getSkill(41120006).getEffect(skilllevel);
/* 221 */             int prop = eff.getProp();
/* 222 */             if (Randomizer.nextInt(100) < prop) {
/* 223 */               eff.applyTo(chr);
             }
           }
/* 226 */         } else if (slea.available() == 9L) {
/* 227 */           slea.skip(1);
/* 228 */           fake = slea.readInt();
         }
       }
/* 231 */       if (chr.getTotalSkillLevel(fake) <= 0) {
/* 232 */         if (chr.isShowPacket()) {
/* 233 */           chr.dropDebugMessage(3, "[玩家受伤] 受到伤害: " + damage + " 技能ID: " + skillId + " 反射伤害: " + pDamage + " defType: " + defType);
         }
         return;
       }
/* 237 */     } else if (damage < -1) {

/* 239 */       c.announce(MaplePacketCreator.enableActions());


       return;
     }


/* 246 */     if (attacker != null && pPhysical && pDamage > 0 && chr.getTotalSkillLevel(skillId) > 0) {
/* 247 */       Skill skill; MapleStatEffect eff; long bounceDamage; switch (skillId) {
         case 1101006:
/* 249 */           skill = SkillFactory.getSkill(skillId);
/* 250 */           eff = skill.getEffect(chr.getTotalSkillLevel(skillId));
/* 251 */           bounceDamage = Math.min((int)(damage * eff.getY() / 100.0D), attacker.getMobMaxHp() / 2L);
/* 252 */           if (chr.isShowPacket()) {
/* 253 */             chr.dropDebugMessage(1, "[玩家受伤] 受到伤害 - 封包解析: " + damage + " 技能吸收倍率: " + (eff.getX() / 100.0D) + " 被动减少倍率: " + (stats.reduceDamageRate / 100.0D));
           }
/* 255 */           damage = (int)(damage - damage * (eff.getX() / 100.0D + stats.reduceDamageRate / 100.0D));
/* 256 */           if (damage < 1) {
/* 257 */             damage = 1;
           }
/* 259 */           if (chr.isShowPacket()) {
/* 260 */             chr.dropDebugMessage(1, "[玩家受伤] 受到伤害 - 减少后的伤害: " + damage + " 处理反射伤害: " + bounceDamage + " 解析反射伤害: " + pDamage + " 技能ID: " + skillId + " - " + skill.getName());
           }
/* 262 */           if (bounceDamage > pDamage) {
/* 263 */             bounceDamage = pDamage;
           }
/* 265 */           attacker.damage(chr, bounceDamage, true, skillId);
           break;
         default:
/* 268 */           attacker.damage(chr, pDamage, true, skillId);
           break;
       }
/* 271 */     } else if (stats.reduceDamageRate > 0) {
/* 272 */       damage = (int)(damage - damage * stats.reduceDamageRate / 100.0D);
/* 273 */       if (damage < 1) {
/* 274 */         damage = 1;
       }
     }
/* 277 */     MapleStatEffect.apply双重防御(chr);
/* 278 */     chr.getCheatTracker().checkTakeDamage(damage);
/* 279 */     Pair<Double, Boolean> modify = chr.modifyDamageTaken(damage, (MapleMapObject)attacker);
/* 280 */     damage = ((Double)modify.left).intValue();
/* 281 */     if (chr.isShowPacket()) {
/* 282 */       chr.dropDebugMessage(1, "[玩家伤害] 最终受到伤害 " + damage);
     }
/* 284 */     if (damage > 0) {
/* 285 */       if (JobConstants.is米哈尔(chr.getJob()) && chr.getBuffedValue(MapleBuffStat.皇家守护防御) != null && damage == 0) {
/* 286 */         chr.send(EffectPacket.showRoyalGuardAttack());
       }

/* 289 */       chr.getCheatTracker().setAttacksWithoutHit(false);

/* 291 */       chr.handle钢铁之躯();

/* 293 */       if (chr.getBuffedValue(MapleBuffStat.变身效果) != null) {
/* 294 */         chr.cancelMorphs();
       }
/* 296 */       boolean mpAttack = (chr.getBuffedValue(MapleBuffStat.金属机甲) != null && chr.getBuffSource(MapleBuffStat.金属机甲) != 35111003);
/* 297 */       if (chr.getBuffedValue(MapleBuffStat.魔法盾) != null) {
/* 298 */         int hploss = 0, mploss = 0;
/* 299 */         if (isDeadlyAttack) {
/* 300 */           if (stats.getHp() > 1) {
/* 301 */             hploss = stats.getHp() - 1;
           }
/* 303 */           if (stats.getMp() > 1) {
/* 304 */             mploss = stats.getMp() - 1;
           }
/* 306 */           if (chr.getBuffedValue(MapleBuffStat.终极无限) != null) {
/* 307 */             mploss = 0;
           }
/* 309 */           chr.addHPMP(-hploss, -mploss);
         } else {
/* 311 */           mploss = (int)(damage * chr.getBuffedValue(MapleBuffStat.魔法盾).doubleValue() / 100.0D) + mpattack;
/* 312 */           hploss = damage - mploss;
/* 313 */           if (chr.getBuffedValue(MapleBuffStat.终极无限) != null) {
/* 314 */             mploss = 0;
/* 315 */           } else if (mploss > stats.getMp()) {
/* 316 */             mploss = stats.getMp();
/* 317 */             hploss = damage - mploss + mpattack;
           }
/* 319 */           chr.addHPMP(-hploss, -mploss);
         }
/* 321 */       } else if (chr.getTotalSkillLevel(27000003) > 0 || chr.getTotalSkillLevel(12000024) > 0) {
/* 322 */         int hploss = 0, mploss = 0;
/* 323 */         if (isDeadlyAttack) {
/* 324 */           if (stats.getHp() > 1) {
/* 325 */             hploss = stats.getHp() - 1;
           }
/* 327 */           if (stats.getMp() > 1) {
/* 328 */             mploss = stats.getMp() - 1;
           }
/* 330 */           chr.addHPMP(-hploss, -mploss);
/* 331 */         } else if (chr.getTotalSkillLevel(27000003) > 0) {
/* 332 */           Skill skill = SkillFactory.getSkill(27000003);
/* 333 */           MapleStatEffect effect = skill.getEffect(chr.getTotalSkillLevel(27000003));
/* 334 */           mploss = (int)(damage * effect.getX() / 100.0D) + mpattack;
/* 335 */           hploss = damage - mploss;
/* 336 */           if (mploss > stats.getMp()) {
/* 337 */             mploss = stats.getMp();
/* 338 */             hploss = damage - mploss + mpattack;
           }
/* 340 */           if (chr.isShowPacket()) {
/* 341 */             chr.dropMessage(5, "[普通魔法防护] 受到伤害: " + damage + " 减少Hp: " + hploss + " 减少Mp: " + mploss + " 技能减少: " + (effect.getX() / 100.0D));
           }
/* 343 */           chr.addHPMP(-hploss, -mploss);
/* 344 */         } else if (chr.getTotalSkillLevel(12000024) > 0) {
/* 345 */           Skill skill = SkillFactory.getSkill(12000024);
/* 346 */           MapleStatEffect effect = skill.getEffect(chr.getTotalSkillLevel(12000024));
/* 347 */           mploss = (int)(damage * effect.getX() / 100.0D) + mpattack;
/* 348 */           hploss = damage - mploss;
/* 349 */           if (mploss > stats.getMp()) {
/* 350 */             mploss = stats.getMp();
/* 351 */             hploss = damage - mploss + mpattack;
           }
/* 353 */           if (chr.isShowPacket()) {
/* 354 */             chr.dropDebugMessage(1, "[火焰斥力] 受到伤害: " + damage + " 减少Hp: " + hploss + " 减少Mp: " + mploss + " 技能减少: " + (effect.getX() / 100.0D));
           }
/* 356 */           chr.addHPMP(-hploss, -mploss);
         }
/* 358 */       } else if (JobConstants.is神之子(chr.getJob())) {
/* 359 */         int skilllevel = chr.getTotalSkillLevel(101120109);
/* 360 */         if (skilllevel > 0) {
/* 361 */           MapleStatEffect effect = SkillFactory.getSkill(101120109).getEffect(skilllevel);
/* 362 */           if (effect.makeChanceResult()) {
/* 363 */             effect.applyTo(chr);
           }
         }
/* 366 */       } else if ((chr.getStat()).mesoGuardMeso > 0.0D) {
/* 367 */         if (chr.isShowPacket()) {
/* 368 */           chr.dropDebugMessage(1, "[玩家受伤] 受到伤害: " + damage);
         }
/* 370 */         damage = (int)Math.ceil(damage * (chr.getStat()).mesoGuard / 100.0D);
/* 371 */         int mesoloss = (int)(damage * (chr.getStat()).mesoGuardMeso / 100.0D);
/* 372 */         if (chr.isShowPacket()) {
/* 373 */           chr.dropDebugMessage(1, "[玩家受伤] 金钱护盾 - 最终伤害: " + damage + " 减少金币: " + mesoloss);
         }
/* 375 */         if (chr.getMeso() < mesoloss) {
/* 376 */           chr.gainMeso(-chr.getMeso(), false);
/* 377 */           chr.cancelBuffStats(new MapleBuffStat[] { MapleBuffStat.金钱护盾 });
         } else {
/* 379 */           chr.gainMeso(-mesoloss, false);
         }
/* 381 */         if (isDeadlyAttack && stats.getMp() > 1) {
/* 382 */           mpattack = stats.getMp() - 1;
         }
/* 384 */         chr.addHPMP(-damage, -mpattack);
/* 385 */       } else if (isDeadlyAttack) {
/* 386 */         chr.addHPMP((stats.getHp() > 1) ? -(stats.getHp() - 1) : 0, (stats.getMp() > 1 && !mpAttack) ? -(stats.getMp() - 1) : 0);
       } else {
/* 388 */         chr.addHPMP(-damage, mpAttack ? 0 : -mpattack);
       }
/* 390 */       if (chr.inPVP() && chr.getStat().getHPPercent() <= 20) {
/* 391 */         SkillFactory.getSkill(PlayerStats.getSkillByJob(93, chr.getJob())).getEffect(1).applyTo(chr);
       }
/* 393 */       if (chr.getBuffedValue(MapleBuffStat.心魂本能) != null && chr.getSpecialStat().getPP() > 0) {
/* 394 */         chr.gainPP(-1);
       }
     }
/* 397 */     byte offset = 0;
/* 398 */     int offset_d = 0;
/* 399 */     if (slea.available() == 1L) {
/* 400 */       offset = slea.readByte();
/* 401 */       if (offset == 1 && slea.available() >= 4L) {
/* 402 */         offset_d = slea.readInt();
       }
/* 404 */       if (offset < 0 || offset > 2) {
/* 405 */         offset = 0;
       }
     }
/* 408 */     if (chr.isShowPacket()) {
/* 409 */       chr.dropDebugMessage(1, "[玩家受伤] 类型: " + type + " 怪物ID: " + monsteridfrom + " 伤害: " + damage + " fake: " + fake + " direction: " + direction + " oid: " + oid + " offset: " + offset);
     }
/* 411 */     chr.hadnle忍耐之盾(damage);
/* 412 */     if (damage < -1 || damage > 200000) {
/* 413 */       if (ServerConfig.DEBUG_MODE) {
/* 414 */         log.error("掉血错误", "玩家[" + chr.getName() + " 职业: " + chr.getJob() + "]掉血错误 - 类型: " + type + " 怪物ID: " + monsteridfrom + " 伤害: " + damage + " fake: " + fake + " direction: " + direction + " oid: " + oid + " offset: " + offset + " 封包:" + slea.toString(true));
       }

       return;
     }

/* 420 */     chr.getMap().broadcastMessage(chr, MaplePacketCreator.damagePlayer(chr.getId(), type, damage, monsteridfrom, direction, skillId, pDamage, pPhysical, pOid, pType, pPos, offset, offset_d, fake), false);
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\TakeDamageHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */